<%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <script type="text/javascript">
  		var path = "<%=basePath%>";
  	</script>
    <title>Node Tree</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" href="<%=basePath%>resources/tree/css/org_style.css" />
	<!-- 动态树 -->
	<jsp:include page="/resources/resource-refer/component-dynamictree-reference.jsp" />
	<!-- 系统通用资源 -->
	<jsp:include page="/resources/resource-refer/common-resource-reference.jsp" />
	<!-- 用户访问菜单路径 -->
	<jsp:include page="/resources/usertrace/common-uservisit-reference.jsp" />
  </head>
  <script type="text/javascript">
  	function hideMenus(menu){
		menu.hideItem("node_ADD");
		menu.hideItem("node_EDIT");
		menu.hideItem("node_DELETE");
		menu.hideContextMenu();
	}
	
	function showOperMenus(tree, menu, id) {
		menu.showItem("node_ADD");
		menu.showItem("node_EDIT");
		menu.showItem("node_DELETE");
	}
  </script>
  <body style="height: 100%;width: 100%;overflow: no;">
    <table border="0" cellpadding="0" width="100%" height="100%" cellspacing="0" class="zuo-left">
		<tr><td id="menuMgr_tree" ></td></tr>
	</table>
	<script type="text/javascript">
		var tree;
		tree = new dhtmlXTreeObject("menuMgr_tree","100%","100%",0);
		tree.setImagePath(path+"resources/tree/org_tree_img/");
		$.getJSON(path+"basic/menuActionJson!queryRootMenuTree.do?ifQueryAuthorityMenu=false&date="+new Date(),function(data){
			tree.loadJSONObject(data);
		});
		//子节点数据异步加载
		tree.attachEvent("onOpenEnd",function(nodeId, event){
			//event是记录展开/合拢动作，-1为合拢  还有0、1；
			if(event != -1) {
				tree.deleteChildItems(nodeId);//删除当前节点下的所有子节点
				$.getJSON(path+"basic/menuActionJson!queryChildMenuTree.do?ifQueryAuthorityMenu=false&date="+new Date(), {
					menuID: nodeId
				}, function(data){
					tree.loadJSONObject(data);
				});
			}
		});
		//增加右键菜单
		var menu = new dhtmlXMenuObject();
		menu.setImagePath(path+"resources/tree/org_tree_img/");
		menu.setIconsPath(path+"resources/tree/org_tree_img/");
		menu.loadXML(path+"basic/treeXml/menuTreeMgr.xml?e="+new Date().getTime());
		menu.addContextZone("menuMgr_tree");
		menu.renderAsContextMenu();
		tree.enableContextMenu(menu);
		//设置鼠标右键单击选中节点
		tree.setOnRightClickHandler(function(id, event) {
			//选中节点
			tree.selectItem(id,"","");
			//菜单设置
			showOperMenus(tree, menu, id);
			//设置菜单加载区域
  			event = event || window.event;
  			menu.addContextZone(tree.getItem(id).span.id);
  			var x = event.pageX || (event.clientX+(document.documentElement.scrollLeft || document.body.scrollLeft));
  			var y = event.pageY || (event.clientY+(document.documentElement.scrollTop || document.body.scrollTop));
  			menu.showContextMenu(x,y);
		});
		//增加右键菜单单击事件
		menu.attachEvent("onClick",function(id){
			//选中节点的节点ID和节点类型
			var pNodeID = tree.getSelectedItemId();
			//节点增删改
			if(id=="node_ADD"){ //新增节点
				loadNewModal(path+"basic/menuAction!queryMenuForManage.do?parentMenuID="+pNodeID+"&date="+new Date(),"Add a node","400px","550px");
			}else if(id=="node_EDIT" && pNodeID!=""){ //编辑节点
				//获取节点层级
				var level = tree.getLevel(pNodeID);
				var parentMenuID = "";
				if(level > 1) {
					parentMenuID = tree.getParentId(pNodeID);
				}
				loadNewModal(path+"basic/menuAction!queryMenuForManage.do?menuID="+pNodeID+"&parentMenuID="+parentMenuID+"&date="+new Date(),"Edit node","400px","550px");
			}else if(id=="node_DELETE" && pNodeID!=""){ //删除节点
				Dialog.confirm("Are you sure to delete this item?", function(){
					$.getJSON(path+"basic/menuActionJson!deleteMenuByID.do?date="+new Date(), {
		  	  			menuID: pNodeID
		  	  		}, function(data) {
		  	  			if(data.success) {
		  	  				Dialog.alert("Successful Operation！", function() {
			  	  				window.location = path + "basic/jsp/menuMgrTree.jsp?date="+new Date();
			  	  				parent.window.frames["tempSwitch"].location = path + "basic/menuAction!queryByParentMenuID.do?date="+new Date();
		  	  				});
		  	  			} else {
		  	  				Dialog.alert(data.msg);
		  	  			}
		  	  		});
				});
			}
		});
		//设置树节点鼠标双击响应
		tree.setOnDblClickHandler(function(nodeID){
			//选中双击树节点
			tree.selectItem(nodeID,"","");
			//读取子菜单列表
			parent.window.frames["tempSwitch"].location=path+"basic/menuAction!queryByParentMenuID.do?parentMenuID="+nodeID+"&date="+new Date();
		});
	</script>
  </body>
</html>
